package arcane.object.gui;

import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;

import arcane.object.game.sprite.MenuSprite;

public class DefaultPanel extends MenuSprite {
	List<MenuSprite> children;
	Insets inset;

	public DefaultPanel() {
		this(null);
	}

	public DefaultPanel(BufferedImage img) {
		this(img, 0, 0);
	}

	public DefaultPanel(BufferedImage img, int x, int y) {
		super(img, x, y);
		children = new ArrayList<MenuSprite>();
		inset = new Insets(3, 3, 3, 3);
	}

	public void add(MenuSprite s) {
		s.setLocation(getX() + inset.left, getY() + inset.top);
		children.add(s);
	}

	public void removeAll() {
		children.clear();
	}

	public void setInsets(Insets inset) {
		this.inset = inset;
	}

	public Insets getInset() {
		return inset;
	}

	public void renderBackground(Graphics2D g, int x, int y) {
		g.setColor(color);
		g.fillRect(x, y, width, height);
	}

	public void render(Graphics2D g, int x, int y) {
		if (getImage() == null) {
			renderBackground(g, x, y);
		} else
			super.render(g, x, y);
		if (isActive())
			renderChildren(g);
	}

	public void renderChildren(Graphics2D g) {
		int x = 0, y = (int) getY() + inset.top;
		for (int i = 0; i < children.size(); i++) {
			MenuSprite s = children.get(i);
			if (s != null) {
				x = (int) getX() + (getWidth() - s.getWidth()) / 2;
				s.setLocation(x, y);
				s.render(g);
				y += s.getHeight();
			}
		}
	}

	public void update(long time) {
		super.update(time);
		if (this.enabled) {
			for (int i = 0; i < children.size(); i++) {
				children.get(i).update(time);
			}
		}
	}

	public void mouseDown(int x, int y) {

	}

	public void mouseExited(int x, int y) {

	}

	public void mouseClicked(int x, int y) {

	}

	public void mouseEntered(int x, int y) {

	}
}